/**
 * 
 */
package com.xb.cloud.product.merchant.inner.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.xb.cloud.common.JsonUtils;
import com.xb.cloud.product.merchant.inner.service.ProductSkuPriceInnerService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.product.merchant.service.impl.AbstractService;
import com.xb.cloud.product.merchant.service.impl.ProductSkuPriceServiceImpl;
import com.xb.cloud.product.service.business.model.ProductSkuPrice;
import com.xb.cloud.product.service.business.model.ProductSkuPriceExample;
import com.xb.cloud.product.service.business.model.mapper.ProductSkuPriceMapper;

/**
 * sku价格服务类
 * 
 * @author kexue
 *
 */

@Service
public class ProductSkuPriceInnerServiceImpl extends AbstractService implements ProductSkuPriceInnerService {
	
	private static final Logger LOGGER=LoggerFactory.getLogger(ProductSkuPriceServiceImpl.class);

	@Resource
	private ProductSkuPriceMapper skuPriceMapper;
	

	@Override
	public Long createSkuPrice(BigDecimal priceMarket, BigDecimal priceSale,Long productId, Long productSkuId,
			EndMerchantUserBo endMerchantUserBo) {

		ProductSkuPrice skuPrice = new ProductSkuPrice();
		skuPrice.setCompanyId(endMerchantUserBo.getCompanyId());

		Long generateKey = ipKeyGenerator.generateKey();
		skuPrice.setId(generateKey); 
		skuPrice.setSkuId(productSkuId);
		skuPrice.setCompanyId(endMerchantUserBo.getCompanyId());
		skuPrice.setMerchantId(endMerchantUserBo.getMerchantId()); 
		skuPrice.setShopId(endMerchantUserBo.getShopId()); 
		skuPrice.setProductId(productId);
		skuPrice.setPriceMarket(priceMarket);
		skuPrice.setPriceSale(priceSale); 
		
		
		skuPrice.setCreateTime(timeService.getDate());
		skuPrice.setCreateUserId(endMerchantUserBo.getId());
		skuPrice.setCreateUserName(endMerchantUserBo.getLoginName());
		int insertSelective = skuPriceMapper.insertSelective(skuPrice);
		if (insertSelective <= 0) {
			String message = ("插入sku price失败" + JsonUtils.beanToJson(skuPrice));
			LOGGER.error(message);
			throw new RuntimeException(message); 
		}
		return generateKey;

	}
	
	//修改sku价格
	@Override
	public Long updateSkuPrice(BigDecimal priceSale,BigDecimal priceMarket,Long productSkuId,
			EndMerchantUserBo endMerchantUserBo) {
		ProductSkuPriceExample example=new ProductSkuPriceExample();
		example.createCriteria()
			.andSkuIdEqualTo(productSkuId)
			.andCompanyIdEqualTo(endMerchantUserBo.getCompanyId())
			.andShopIdEqualTo(endMerchantUserBo.getShopId())
			.andMerchantIdEqualTo(endMerchantUserBo.getMerchantId());
		List<ProductSkuPrice> oldList= skuPriceMapper.selectByExample(example);
		if(oldList.size()==0){
			String message = ("更新sku price失败");
			LOGGER.error(message);
			throw new RuntimeException(message); 
		}
		ProductSkuPrice old=oldList.get(0);
		old.setPriceSale(priceSale);
		old.setPriceMarket(priceMarket);
		old.setUpdateTime(timeService.getDate());
		old.setUpdateUserId(endMerchantUserBo.getId());
		old.setUpdateUserName(endMerchantUserBo.getLoginName());
		int affected = skuPriceMapper.updateByExampleSelective(old, example);
		if (affected <= 0) {
			String message = ("更新sku price失败" + JsonUtils.beanToJson(old));
			LOGGER.error(message);
			throw new RuntimeException(message); 
		}
		return old.getId();
	}

}
